package javaLearning.拷贝;

import 设计模式.单例模式.破坏单例模式.Singleton;

import java.io.*;

/**
 * @author : K k
 * @date : 23:05 2020/9/1
 */
public class DeepCopy {

    public static void main(String[] args) throws IOException, ClassNotFoundException{
        User user=new User("lllx");
        User user3=new User("llxxxxa");
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(bao);
        oos.writeObject("Worm storage By ByteOutputStream ");
        oos.writeObject(user);
        oos.flush();
        ByteArrayInputStream bai=new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois=new ObjectInputStream(bai);
        String s2 = (String)ois.readObject();
        User user2=(User)ois.readObject();
        ois.close();
        System.out.println(s2);
        System.out.println(user2==user);
        System.out.println(user2.name.equals(user.name));
        System.out.println(user2);
        System.out.println(user.singleton==user2.singleton);
        System.out.println(user.singleton==user3.singleton);
    }

    static class User implements Serializable{
        public String name;
        public static int x=10;
        public Singleton singleton=Singleton.getSingleton();
        User(String name){
            this.name=name;
            System.out.println("我是"+name);
        }
    }

}
